﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;



namespace UtilityXml
{
    public class XmlForm
    {
        XmlDocument form;
        public string name{get; set;}

        
        public XmlForm(string formName)
        {
            name = formName;
            form = new XmlDocument();
            //XmlDeclaration xDecl = form.CreateXmlDeclaration(formName, , null);
            XmlElement xmlRootForm = form.CreateElement("", formName, "");
            form.AppendChild(xmlRootForm);            
        }

        
        
        public void addWidget(Control c)
        {
            XmlNode root = form.SelectSingleNode(name);
            XmlNode newWidget = form.CreateNode(XmlNodeType.Element, "Widget", null);
            XmlAttribute attrName = form.CreateAttribute("Name");
            attrName.Value = c.Name;
            newWidget.Attributes.Append(attrName);
            XmlAttribute attrType = form.CreateAttribute("Type");
            attrType.Value = c.GetType().ToString();
            c.GetType().
            newWidget.Attributes.Append(attrType);
            root.AppendChild(newWidget);
            XmlNode positionX = form.CreateNode(XmlNodeType.Element, "X-Position", null);
            positionX.InnerText = c.Location.X.ToString();
            newWidget.AppendChild(positionX);
            XmlNode positionY = form.CreateNode(XmlNodeType.Element, "Y-Position", null);
            positionY.InnerText = c.Location.Y.ToString();
            newWidget.AppendChild(positionY);
        }



        public void modifyWidget(Control c)
        {
            XmlNode temp = form.SelectSingleNode("/" + name + "/Widget[@Name='" + c.Name + "']");
            temp.ChildNodes[0].InnerText = c.Location.X.ToString();
            temp.ChildNodes[1].InnerText = c.Location.Y.ToString();
        }

        public void SaveXml(string path)
        {
            try
            {
                form.Save(path);
            }
            catch (Exception e) 
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
